package com.mycompany.userregistrationform.client;

import com.mycompany.userregistrationform.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class UserRegistrationForm implements EntryPoint {

	  private Label fullNameLabel;
	  private TextBox fullNameField;
	  
	  private Label emailLabel;
	  private TextBox emailField;
	  
	  private Label usernameLabel;
	  private TextBox usernameField;
	  
	  private Label passwordLabel;
	  private PasswordTextBox passwordField;
	  
	  private Label retypePasswordLabel;
	  private PasswordTextBox retypePasswordField;
	  
	  private Label passwordErrorLabel;
	  private Label retypePasswordErrorLabel;
	
	
	  private static final String SERVER_ERROR = "An error occurred while "
			  + "attempting to contact the server. Please check your network "
			  + "connection and try again.";


	  private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	  
	  @SuppressWarnings("deprecation")
	  public void onModuleLoad() {
		  fullNameLabel = new Label("Full Name");
		  fullNameField = new TextBox();
		  
		  emailLabel = new Label("E-mail");
		  emailField = new TextBox();
		  
		  usernameLabel = new Label("Username");
		  usernameField = new TextBox();
		  
		  passwordLabel = new Label("Password");
		  passwordField = new PasswordTextBox();
		  
		  retypePasswordLabel = new Label("Retype Password");
		  retypePasswordField = new PasswordTextBox();
		  
		  passwordErrorLabel = new Label();
		  retypePasswordErrorLabel = new Label();
		  
		  Button submintButton = new Button("Register");
		  
		  RootPanel.get("fullNameLabel").add(fullNameLabel);
		  RootPanel.get("fullNameField").add(fullNameField);
		  RootPanel.get("emailLabel").add(emailLabel);
		  RootPanel.get("emailField").add(emailField);
		  RootPanel.get("usernameLabel").add(usernameLabel);
		  RootPanel.get("usernameField").add(usernameField);
		  RootPanel.get("passwordLabel").add(passwordLabel);
		  RootPanel.get("passwordField").add(passwordField);
		  RootPanel.get("retypePasswordLabel").add(retypePasswordLabel);
		  RootPanel.get("retypePasswordField").add(retypePasswordField);
		  RootPanel.get("passwordErrorLabel").add(passwordErrorLabel);
		  RootPanel.get("retypePasswordErrorLabel").add(retypePasswordErrorLabel);
		  RootPanel.get("submitionButton").add(submintButton);

		  submintButton.addClickListener(new ClickListener() {
			  public void onClick(Widget widget) {
				  if(validateFormInput()){
					  AccountInfo accountInfo = new AccountInfo();
					  accountInfo.setUsername(usernameField.getText());
					  accountInfo.setPassword(passwordField.getText());
					  register(accountInfo);
				  }
			  }
		  });
	  }
	  
	  /**
	   * Valida a entrada do formulário e retorna se é válida ou não.
	   */
	  protected boolean validateFormInput(){
		  boolean passwordIsValid = passwordField.getText().length() > 0;
		  passwordErrorLabel.setText(passwordIsValid?"":"Required");
		  boolean retypeIsValid = retypePasswordField.getText().equals(passwordField.getText());
		  retypePasswordErrorLabel.setText(retypeIsValid?"":"Incorrect");
		  
		  return passwordIsValid && retypeIsValid;
	  }
	  
	  /**
	   * Registra o usuário no sistema.
	   * @param accountInfo: objeto contendo os dados do formulário de registro do usuário.
	   */
	  private void register(AccountInfo accountInfo) {
		  Window.alert(accountInfo.toString());
	  }
}
